package com.jintian.smart.platform.system.service;

import com.jintian.smart.kernel.common.util.SmartBeanUtil;
import com.jintian.smart.kernel.common.util.StringUtil;
import com.jintian.smart.kernel.core.domain.ResponseDTO;
import com.jintian.smart.kernel.orm.impl.AbstractEntityServiceImpl;
import com.jintian.smart.base.api.system.entity.PositionEntity;
import com.jintian.smart.base.api.system.form.PositionAddForm;
import com.jintian.smart.base.api.system.form.PositionQueryForm;
import com.jintian.smart.base.api.system.form.PositionUpdateForm;
import com.jintian.smart.base.api.system.view.PositionVO;
import com.jintian.smart.platform.system.mapper.PositionMapper;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.beetl.sql.core.page.PageResult;
import org.beetl.sql.core.query.LambdaQuery;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 职务表 Service
 */
@Service
public class PositionService extends AbstractEntityServiceImpl<PositionMapper, PositionEntity> {
    @Resource
    private PositionMapper positionMapper;

    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<PositionVO> queryPage(PositionQueryForm queryForm) {
        LambdaQuery<PositionEntity> query = positionMapper.createLambdaQuery().andEq(PositionEntity::getDeletedFlag, 0);
        if (StringUtil.isNotBlank(queryForm.getKeywords())) {
            query.andLike(PositionEntity::getPositionName, "%" + queryForm.getKeywords() + "%");
        }
        PageResult<PositionEntity> pageResult = query.page(queryForm.getPageNo(), queryForm.getPageSize());
        PageResult<PositionVO> voResult = SmartBeanUtil.copyPage(pageResult, PositionVO.class);
        return voResult;
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(PositionAddForm addForm) {
        PositionEntity positionEntity = SmartBeanUtil.copy(addForm, PositionEntity.class);
        positionMapper.insert(positionEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(PositionUpdateForm updateForm) {
        PositionEntity positionEntity = SmartBeanUtil.copy(updateForm, PositionEntity.class);
        positionMapper.updateById(positionEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        this.deleteByIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Long positionId) {
        if (null == positionId) {
            return ResponseDTO.ok();
        }

        positionMapper.deleteById(positionId);
        return ResponseDTO.ok();
    }

    /**
     * 分页查询
     *
     * @return
     */
    public List<PositionVO> queryList() {
        List<PositionEntity> entityList = positionMapper.createLambdaQuery().andEq(PositionEntity::getDeletedFlag, 0).select();
        List<PositionVO> list = SmartBeanUtil.copyList(entityList, PositionVO.class);
        return list;
    }
}
